package Model;

/*CREATE  TABLE IF NOT EXISTS `mydb`.`Licenca` (
  `idLicenca` INT NULL AUTO_INCREMENT ,
  `Licencas_idLicencas` INT NOT NULL ,
  `quantidade` INT NULL ,
  `serial` VARCHAR(100) NULL ,
  PRIMARY KEY (`idLicenca`) ,
  INDEX `fk_Licenca_Licencas1` (`Licencas_idLicencas` ASC) ,
  CONSTRAINT `fk_Licenca_Licencas1`
    FOREIGN KEY (`Licencas_idLicencas` )
    REFERENCES `mydb`.`Licencas` (`idLicencas` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'Tipo, quantidade, etc.';*/
public class Licenca {
    //<editor-fold defaultstate="collapsed" desc="Connection">
    private static com.mysql.jdbc.Connection connection;

    public static com.mysql.jdbc.Connection getConnection() {
        return connection;
    }

    public static void setConnection(com.mysql.jdbc.Connection connection) {
        Licenca.connection = connection;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Variable Declaration">
    private int idLicenca;
    private Licencas Licencas_idLicencas;
    private int quantidade;
    private String serial;
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public Licenca(int idLicenca) {
        try {
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery("SELECT * FROM itens WHERE idLicenca = '" + idLicenca + "'");
            rs.next();
            this.idLicenca = rs.getInt("idLicenca");
            this.Licencas_idLicencas = new Licencas(rs.getInt("Licencas_idLicencas"));
            this.quantidade = rs.getInt("quantidade");
            this.serial = rs.getString("serial");
        } catch (Exception e) {
        }
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="toString">
    @Override
    public String toString() {
        String s="Licenca:";
            s += "idLicenca=" + idLicenca;
        return s;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Getter">
    public int idLicenca() {
        return this.idLicenca;
    }

    public Licencas Licencas_idLicencas() {
        return this.Licencas_idLicencas;
    }

    public int quantidade() {
        return this.quantidade;
    }

    public String serial() {
        return this.serial;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Setter">
    public boolean idLicenca(int idLicenca) {
        boolean ok = update("idLicenca", idLicenca);
        if (ok) {
            this.idLicenca = idLicenca;
        }
        return ok;
    }
    public boolean Licencas_idLicencas(Licencas Licencas_idLicencas) {
        boolean ok = update("Licencas_idLicencas", Licencas_idLicencas.idLicencas());
        if (ok) {
            this.Licencas_idLicencas = Licencas_idLicencas;
        }
        return ok;
    }
    public boolean quantidade(int quantidade) {
        boolean ok = update("quantidade", quantidade);
        if (ok) {
            this.quantidade = quantidade;
        }
        return ok;
    }
    public boolean serial(String serial) {
        boolean ok = update("serial", serial);
        if (ok) {
            this.serial = serial;
        }
        return ok;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Update">
    /**
     *
     * @param campo
     * @param valor
     * @return
     */
    public boolean update(String campo, Object valor) {
        try {
            String sql = "UPDATE Licenca \n"
                    + "SET " + campo + " = '";
            if (valor instanceof String) {
                sql += (String) valor;
            } else if (valor instanceof Integer) {
                sql += (Integer) valor;
            } else if (valor instanceof java.sql.Timestamp) {
                sql += ((java.sql.Timestamp) valor).toString().split("\\.")[0];
            } else if (valor instanceof java.util.Date) {
                java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
                sql += sdf.format((java.util.Date) valor);
            } else {
                sql += valor.toString();
            }
            sql += "'\n WHERE \n idLicenca = '" + idLicenca + "'";
            com.mysql.jdbc.Statement st =
                    (com.mysql.jdbc.Statement) connection.createStatement();
            int x = st.executeUpdate(sql);
            if (x == 1) {
                return true;
            }
        } catch (Exception ex) {
        }
        return false;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Search">
    public static java.util.ArrayList<Licenca> searchIdLicenca(int valor) {
        return search("idLicenca", valor+"");
    }
    public static java.util.ArrayList<Licenca> searchLicencas_idLicencas(Licencas valor) {
        return search("Licencas_idLicencas", valor.idLicencas()+"");
    }
    public static java.util.ArrayList<Licenca> searchQuantidade(int valor) {
        return search("quantidade", valor+"");
    }
    public static java.util.ArrayList<Licenca> searchSerial(String valor) {
        return search("serial", valor+"");
    }

    public static java.util.ArrayList<Licenca> search(String campo, String valor) {
        try {
            java.util.ArrayList<Licenca> retorno = new java.util.ArrayList<>();
            String sql = "SELECT idLicenca FROM Licenca "
                    + "WHERE " + campo + " LIKE '%" + valor + "%' "
                    + "ORDER BY " + campo;
            com.mysql.jdbc.ResultSet rs = (com.mysql.jdbc.ResultSet) connection.createStatement().executeQuery(sql);
            while (rs.next()) {
                retorno.add(new Licenca(rs.getInt("idLicenca")));
            }
            return retorno;
        } catch (Exception e) {
        }
        return null;
    }
    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="New">
    public static Licenca newLicenca(int idLicenca, Licencas Licencas_idLicencas, int quantidade, String serial){
            String sql = "INSERT INTO Licenca (idLicenca, Licencas_idLicencas, quantidade, serial)"
                    + " VALUES "
                    + "('"+idLicenca+"', '"+Licencas_idLicencas.idLicencas() + "', '"+quantidade+"', '"+serial+"');";
        com.mysql.jdbc.Statement stmt = null;
        com.mysql.jdbc.ResultSet rs = null;
        try {
            stmt = (com.mysql.jdbc.Statement) connection.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                    java.sql.ResultSet.CONCUR_UPDATABLE);
            stmt.executeUpdate(sql, com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            rs = (com.mysql.jdbc.ResultSet) stmt.getGeneratedKeys();
            if (rs.next()) {
                return new Licenca(rs.getInt("idLicenca"));
            } else {
                return new Licenca(idLicenca);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
        return null;
    }
    //</editor-fold>
}